﻿namespace ufida.u9.ait.openapi
{
	using System;
    using System.Collections;
    using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.CBO.HR.Operator;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.CBO.SCM.Warehouse;
    using UFIDA.U9.ISV.RCV.DTO;
    using UFIDA.U9.ISV.RCV.Proxy;
    using UFIDA.U9.PM.PO;
    using UFIDA.U9.PM.Rcv;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

	/// <summary>
	/// RCVSaveFromPO partial 
	/// </summary>	
	public partial class RCVSaveFromPO 
	{	
		internal BaseStrategy Select()
		{
			return new RCVSaveFromPOImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class RCVSaveFromPOImpementStrategy : BaseStrategy
	{
		public RCVSaveFromPOImpementStrategy() { }

		public override object Do(object obj)
		{						
			RCVSaveFromPO bpObj = (RCVSaveFromPO)obj;

			try
			{
				if (bpObj.Data == null || bpObj.Data.Count == 0)
					throw new Exception("传入参数为空");
				List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();

				//List<object> inputdtos = DTOUtils.DTOListPropCopy(bpObj.Data.ConvertAll(item => item as object)
				//	, typeof(POToRcvTransDTOData));
				foreach (var dto in bpObj.Data)
                {
					Receivement entity = Receivement.Finder.Find($"DocNo='{dto.DocNo}'");
                    if (entity == null)
                    {
						PreSave(dto);

						List<object> inputdtos=DTOUtils.DTOListPropCopy(dto.RcvLines.ConvertAll(item => item as object)
							, typeof(POToRcvTransDTOData));
						//新增逻辑
						CeateReceivementFromPOSRVProxy proxy = new CeateReceivementFromPOSRVProxy();
						proxy.POToRcvTransDTOs = inputdtos.ConvertAll(o => o as POToRcvTransDTOData);
                        List<long> ret = proxy.Do();
						if(ret.Count==0)
							throw new Exception("生成出货单失败!");

						//修改单号并返回
						retlist.Add(new CommonArchiveDataDTOData
						{
							ID = ret[0],
							Code = EntityUtils.UpdateDocNo<Receivement>(ret[0], dto.DocNo)
						});

						//单据审核
						RCVApproveSV approve = new RCVApproveSV();
						approve.Data = new List<CopyOfCommonArchiveDataDTOData>
						{
							new CopyOfCommonArchiveDataDTOData
							{
								Code =dto.DocNo,
							}
						};
						ResultDTOData resultDTOData = approve.Do();
						ResultUtils.CheckResult(resultDTOData);
					}
                    else
                    {
                        //修改逻辑
                        throw new AITException(599,$"单号:{dto.DocNo}已经存在,不能生成!");
                    }
                }
                return ResultUtils.success(bpObj, bpObj.Data, retlist);
			}
			catch (Exception ex)
			{
				return ResultUtils.error(bpObj, bpObj.Data, ex);
			}
		}

        private void PreSave(POToRcvDocDTOData dto)
        {
			//校验
			if (dto.DocNo == null || dto.DocNo.Length == 0)
				throw new Exception("传入单号为空");
			foreach(var line in dto.RcvLines)
            {
				if (line.ItemMaster == null)
					throw new Exception("传入物料[ItemMaster]为空");
				string path = $"SubLineNo={line.POShipLineNo} and POLine.DocLineNo={line.POLineNo} and POLine.PurchaseOrder.DocNo='{line.PONo}'";

				POShipLine shipLine = POShipLine.Finder.Find(path);
				if (shipLine == null)
					throw new Exception($"查找采购订单行失败!path:{path}");
				if (shipLine.ItemInfo.ItemCode != line.ItemMaster.Code)
					throw new Exception($"来源料号[{shipLine.ItemInfo.ItemCode}]与传入料号[{line.ItemMaster.Code}]不同!");
            }

            //赋值
            foreach (var line in dto.RcvLines)
            {
				Warehouse wh = Warehouse.Finder.Find($"Code='{line.RcvLineInfoDTO.WhCode}'");
				if (wh == null)
				{
					throw new Exception($"仓库编码[{line.RcvLineInfoDTO.WhCode}]查找不到仓库!");
				}
				Operators operators = Operators.Finder.Find($"Code='{line.RcvLineInfoDTO.WhManCode}'");
				if (operators == null)
				{
					throw new Exception($"业务员编码[{line.RcvLineInfoDTO.WhManCode}]查找不到业务员!");
				}
				line.RcvLineInfoDTO.Wh = wh.ID;
				line.RcvLineInfoDTO.WhMan = operators.ID;
			}
			
		}
    }

	#endregion
	
	
}